package com.arm.service.impl;

import com.arm.dao.AdminMenuMapper;
import com.arm.module.AdminMenu;
import com.arm.module.admin.Menu;
import com.arm.service.BaseService;
import com.arm.service.MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/22.
 */
@Service
public class MenuServiceImpl extends BaseService implements MenuService {

    @Autowired
    AdminMenuMapper adminMenuMapper;


    @Override
    public List<AdminMenu> getMenuList(Map<String, String> map) {
        return adminMenuMapper.selectMenuList(map);
    }

    @Override
    public List<AdminMenu> selectMenuListForRole(Map<String, String> map) {
        return adminMenuMapper.selectMenuListForRole(map);
    }

    @Override
    public void saveMenuRole(Map<String, String> map) {


    }

    @Override
    public void addMenu(AdminMenu adminMenu) {

        adminMenuMapper.insertAdminMenu(adminMenu);
    }

    @Override
    public void deleteMenu(Map<String, String> map) {
        String menuIds = map.get("menuIds");
        String menuIdArry[] = menuIds.split(",");
        for (String menuId : menuIdArry) {
            AdminMenu adminMenu = adminMenuMapper.selectMenuById(Long.parseLong(menuId));
            Long menuParentId = adminMenu.getMenuParentId();
            Map<String, String> deletemap = new HashMap<>();
            if (menuParentId.intValue() == 0) {
                deletemap.put("menuParentId", String.valueOf(adminMenu.getTid()));
                adminMenuMapper.deleteAdminMenu(deletemap);
            }
            deletemap.clear();

            deletemap.put("tid", String.valueOf(adminMenu.getTid()));
            adminMenuMapper.deleteAdminMenu(deletemap);
        }
    }

    @Override
    public List<Menu> getMenuListByRole(Map<String, String> map) {

        List<AdminMenu> adminMenuList = adminMenuMapper.selectMenuListForRole(map);
        List<Menu> menuList = new ArrayList<>();
        menuList = setMenu(adminMenuList, 0l, menuList);

        return menuList;
    }

    @Override
    public List<Menu> getALLMenuList(Map<String, String> map) {
        List<AdminMenu> adminMenuList = adminMenuMapper.selectMenuList(map);
        List<Menu> menuList = new ArrayList<>();
        menuList = setMenu(adminMenuList, 0l, menuList);

        return menuList;
    }

    private List<Menu> setMenu(List<AdminMenu> adminMenuList, Long index, List<Menu> menuList) {


        for (AdminMenu adminMenu : adminMenuList
                ) {

            if (adminMenu.getMenuParentId() == index) {
                Menu menu = new Menu();
                menu.setTid(adminMenu.getTid());
                menu.setFlag(false);
                menu.setIcon(adminMenu.getIcon());

                String menuIndex=adminMenu.getIndex();
                if(StringUtils.isBlank(menuIndex)){
                    menuIndex=String.valueOf(adminMenu.getTid());
                }
                menu.setIndex(menuIndex);
                menu.setTitle(adminMenu.getMenuName());

                List<Menu> menuList1=setMenu(adminMenuList, adminMenu.getTid(), new ArrayList<Menu>());
                if(menuList1!=null && menuList1.size()>0){
                    menu.setSubs(menuList1);
                }

                menuList.add(menu);
            }


        }
        return menuList;
    }


}
